
<!--
  * 支付链接管理
  *
  * @Author:    1024创新实验室
  * @Date:      2025-01-18
  * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012
-->
<template>
  <a-card size="small" :bordered="false" :hoverable="false">
    <a-form class="smart-query-form">
      <a-row class="smart-query-form-row">
        <a-form-item label="货币" class="smart-query-form-item">
          <CurrencySelect 
            v-model:value="queryForm.currencyId" 
            placeholder="请选择货币" 
            style="width: 200px"
            @change="onSearch"
          />
        </a-form-item>
        <a-form-item label="关键词" class="smart-query-form-item">
          <a-input 
            v-model:value="queryForm.keywords" 
            placeholder="请输入关键词搜索" 
            style="width: 200px"
            @pressEnter="onSearch"
          />
        </a-form-item>
        <a-form-item class="smart-query-form-item smart-margin-left10">
          <a-button-group>
            <a-button type="primary" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-form-item>
        <a-form-item class="smart-query-form-item smart-margin-left10">
            <a-button type="primary" @click="addLink" class="smart-margin-bottom5">
        <template #icon>
          <PlusOutlined />
        </template>
        新增链接
      </a-button>
        </a-form-item>
      </a-row>
    </a-form>

    <a-row justify="end">
     
    </a-row>

    <a-table
      size="small"
      :loading="tableLoading"
      :dataSource="tableData"
      :columns="columns"
      rowKey="id"
      :pagination="false"
      bordered
    >
      <template #bodyCell="{ text, record, column }">
        <template v-if="column.dataIndex === 'isValid'">
          <a-tag :color="text ? 'success' : 'error'">
            {{ text ? '有效' : '无效' }}
          </a-tag>
        </template>
        <template v-if="column.dataIndex === 'generatedLink'">
          <a :href="text" target="_blank" class="link-text">{{ text }}</a>
        </template>
        <template v-if="column.dataIndex === 'expireTime'">
          {{ formatDate(text) }}
        </template>
        <template v-if="column.dataIndex === 'createTime'">
          {{ formatDate(text) }}
        </template>
        <template v-if="column.dataIndex === 'updateTime'">
          {{ formatDate(text) }}
        </template>
        <!-- <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="editLink(record)" type="link">编辑</a-button>
            <a-button @click="deleteLink(record)" danger type="link">删除</a-button>
          </div>
        </template> -->
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNum"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>

    <!-- 新增/编辑弹窗 -->
    <LinkFormModal ref="formModalRef" @reloadList="queryData" />
  </a-card>
</template>

<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { SearchOutlined, ReloadOutlined, PlusOutlined } from '@ant-design/icons-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { queryPageGenerateLinkRecord, updateGenerateLinkRecord } from '/@/api/pay/pay-api';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { smartSentry } from '/@/lib/smart-sentry';
  import CurrencySelect from '/@/components/system/currency-select/index.vue';
  import LinkFormModal from './components/link-form-modal.vue';
  import dayjs from 'dayjs';

  // =========== 表格列定义 =============
  const columns = ref([
    {
      title: 'ID',
      dataIndex: 'id',
      width: 60,
    },
    {
      title: '生成链接',
      dataIndex: 'paymentLink',
      width: 250,
      ellipsis: true,
    },

    {
      title: '货币代码',
      dataIndex: 'currencyCode',
      width: 90,
    },
    {
      title: '过期时间',
      dataIndex: 'expireTime',
      width: 150,
    },
    {
      title: '是否有效',
      dataIndex: 'isValid',
      width: 90,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 150,
    },
    // {
    //   title: '更新时间',
    //   dataIndex: 'updateTime',
    //   width: 180,
    // },
    {
      title: '备注',
      dataIndex: 'remark',
      ellipsis: true,
      width: 180,
    },
    // {
    //   title: '操作',
    //   dataIndex: 'action',
    //   fixed: 'right',
    //   width: 120,
    // },
  ]);

  // =========== 查询表单 =============
  const queryFormState = {
    currencyId: undefined,
    keywords: undefined,
    pageNum: 1,
    pageSize: 10,
  };
  const queryForm = reactive({ ...queryFormState });

  // =========== 表格数据 =============
  const tableData = ref([]);
  const tableLoading = ref(false);
  const total = ref(0);
  const formModalRef = ref();

  // =========== 查询数据 =============
  async function queryData() {
    try {
      tableLoading.value = true;
      const params = {
        ...queryForm,
        pageNum: queryForm.pageNum,
        pageSize: queryForm.pageSize,
      };
      const res = await queryPageGenerateLinkRecord(params);
      tableData.value = res.data.list || [];
      total.value = res.data.total || 0;
    } catch (e) {
      smartSentry.captureError(e);
      message.error('查询失败');
    } finally {
      tableLoading.value = false;
    }
  }

  // =========== 搜索和重置 =============
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    queryData();
  }

  // =========== 新增和编辑 =============
  function addLink() {
    formModalRef.value?.showModal();
  }

  function editLink(record) {
    formModalRef.value?.showModal(record);
  }

  // =========== 删除 =============
  function deleteLink(record) {
    Modal.confirm({
      title: '确认删除',
      content: '确定要删除这条链接记录吗？',
      onOk: async () => {
        try {
          await updateGenerateLinkRecord({ id: record.id, deletedFlag: true });
          message.success('删除成功');
          queryData();
        } catch (e) {
          smartSentry.captureError(e);
          message.error('删除失败');
        }
      },
    });
  }

  // =========== 工具函数 =============
  function formatDate(dateStr) {
    if (!dateStr) return '-';
    return dayjs(dateStr).format('YYYY-MM-DD HH:mm:ss');
  }

  // =========== 生命周期 =============
  onMounted(() => {
    queryData();
  });
</script>

<style lang="less" scoped>
  .link-text {
    color: #1890ff;
    text-decoration: none;
    &:hover {
      text-decoration: underline;
    }
  }
</style>
  